<script lang="ts" setup>
// import type { DemoTableApi } from '../mock-api';

import type { VxeGridProps } from '#/adapter/vxe-table';

import { useVbenModal } from '@vben/common-ui';

import { ElButton, ElOption, ElSelect } from 'element-plus';

import { useVbenForm } from '#/adapter/form';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
// import { MOCK_API_DATA } from '../table-data';

import VideoUpload from '#/components/VideoUpload.vue';

import { tagListApi } from '#/api';
interface RowType {
  category: string;
  color: string;
  id: string;
  price: string;
  productName: string;
  releaseDate: string;
}

// 数据实例
// const MOCK_TREE_TABLE_DATA = [
//   {
//     date: '2020-08-01',
//     id: 10_000,
//     name: 'Test1',
//     parentId: null,
//     size: 1024,
//     type: 'mp3',
//   },
// ]

const sleep = (time = 1000) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

/**
 * 获取示例表格数据
 */
function getExampleTableApi(params) {
  // return new Promise<{ items: any; total: number }>((resolve) => {
  //   const { page, pageSize } = params;
  //   const items = MOCK_API_DATA.slice((page - 1) * pageSize, page * pageSize);

  //   sleep(1000).then(() => {
  //     resolve({
  //       total: items.length,
  //       items,
  //     });
  //   });
  // });
  return {
    total: 1,
    items: [],
  };
}

const gridOptions: VxeGridProps<RowType> = {
  checkboxConfig: {
    highlight: true,
    labelField: 'name',
  },
  columns: [
    { title: '序号', type: 'seq', width: 50 },
    { field: 'tagCode', title: '标签码' },
    { field: 'tagName', title: '标签名' },
  ],
  exportConfig: {},
  // height: 'auto', // 如果设置为 auto，则必须确保存在父节点且不允许存在相邻元素，否则会出现高度闪动问题
  keepSource: true,
  proxyConfig: {
    ajax: {
      query: async ({ page }) => {
        return await tagListApi({
          type: 'CARBRAND',
          offset: page.currentPage,
          size: page.pageSize,
        });
      },
    },
  },
  toolbarConfig: {
    custom: true,
    export: true,
    // import: true,
    refresh: true,
    zoom: true,
  },
};

const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions,
});
const options = [
  { value: 'A', label: '选项A' },
  { value: 'B', label: '选项B' },
  { value: 'C', label: '选项C' },
];
const [BaseForm, formApi] = useVbenForm({
  showDefaultActions: false,
  commonConfig: {
    // 所有表单项
    componentProps: {
      class: 'w-full',
    },
  },
  schema: [
    // {
    //   component: 'Select',
    //   fieldName: 'tagCodes',
    //   label: '标签编码',
    //   componentProps: {
    //     filterable: true,
    //     multiple: true,
    //     options: [
    //       { value: 'A', label: '选项A' },
    //       { value: 'B', label: '选项B' },
    //       { value: 'C', label: '选项C' },
    //     ],
    //   },
    // },
    {
      component: 'Select',
      fieldName: 'relImgMaterialIds',
      label: '关联图片',
    },
  ],
});
const [Modal, modalApi] = useVbenModal({
  onConfirm: () => {
    formApi.getValues().then((res) => {});
  },
});
</script>

<template>
  <div class="vp-raw w-full">
    <Grid>
      <template #toolbar-tools>
        <ElButton class="mr-2" type="primary" @click="() => modalApi.open()">
          添加视频素材
        </ElButton>
      </template>
    </Grid>
    <Modal class="w-[600px]" title="添加视频素材">
      <div class="flex justify-center">
        <VideoUpload />
      </div>
      <BaseForm>
        <template #relImgMaterialIds="data">
          <ElSelect
            :filterable="true"
            :multiple="true"
            v-bind="data"
            :multiple-limit="1"
          >
            <ElOption
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
              <div>
                <img :alt="item.label" src="" srcset="" />
              </div>
            </ElOption>
          </ElSelect>
        </template>
      </BaseForm>
    </Modal>
  </div>
</template>
